﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AppCode_Admin_AddInsuranceCompany : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["userNameAdmin"] == null)
            {
                Response.Redirect("..\\Home\\HomePage.aspx");
            }
        }
        Label5.Visible = false;
        Label6.Visible = false;
        Label7.Visible = false;
        Label8.Visible = false;
        Label9.Visible = false;
        success.Visible = false;

    }
    public void AddInsurance(String name, String address, String phone, String url)
    {
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("Insert into CompanyDetails values(@Name,@Address,@Phone,@Url)", sqlConnect);
        sqlCommand.Parameters.AddWithValue("@Name", name);
        sqlCommand.Parameters.AddWithValue("@Address", address);
        sqlCommand.Parameters.AddWithValue("@Phone", phone);
        sqlCommand.Parameters.AddWithValue("@Url", url);
        sqlCommand.ExecuteNonQuery();
        sqlConnect.Close();
    }

    public Boolean CheckExistingCompany(String name)
    {
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("select *from CompanyDetails", sqlConnect);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        while (reader.Read())
        {
            String Companyname = reader.GetString(1);
            if (Companyname.Equals(name))
            {
                sqlConnect.Close();
                return true;
            }
        }
        sqlConnect.Close();
        return false;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Boolean check = false;
        if (txtName.Text.Equals(""))
        {
            Label5.Visible = true;
            check = true;
        }
        else if (CheckExistingCompany(txtName.Text))
        {
            Label9.Visible = true;
        }

        if (txtAddress.Text.Equals(""))
        {
            Label6.Visible = true;
            check = true;
        }
        if (txtPhone.Text.Equals(""))
        {
            Label7.Visible = true;
            check = true;
        }
        if (txtUrl.Text.Equals(""))
        {
            Label8.Visible = true;
            check = true;
        }
        if (!check)
        {
            this.AddInsurance(txtName.Text, txtAddress.Text, txtPhone.Text, txtUrl.Text);
            success.Visible = true;
            txtName.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            txtUrl.Text = "";
        }
    }

}